package com.nf.health.app.intervention_details.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.intervention_details.bean.FoodDetailsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 恩福 on 2016/11/14.
 * 膳食方案 Adapter
 *
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class FoodDetailsAdapter extends RecyclerView.Adapter {
    public static final int TYPE_HEAD_INFO = 0;
    public static final int TYPE_SUB_TITLE = 1;
    public static final int TYPE_RECOMMEND_RECIPE = 2;
    public static final int TYPE_TEXT_CP = 5;
    public static final int TYPE_FOOTER = 6;

    public static final int ITEM_BREAKFAST = 2;
    public static final int ITEM_LUNCH = 3;
    public static final int ITEM_DINNER = 4;
    private FoodDetailsBean foodDetailsBean;

    private Context mContext;

    public FoodDetailsAdapter(Context context, FoodDetailsBean foodDetailsBean) {
        this.mContext = context;
        this.foodDetailsBean = foodDetailsBean;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_HEAD_INFO:
                view = LayoutInflater.from(mContext).inflate(R.layout.scheme_common_head_info, parent, false);
                return new HeadInfoHolder(view);
            case TYPE_SUB_TITLE:
                view = LayoutInflater.from(mContext).inflate(R.layout.scheme_common_text_subtitle, parent, false);
                return new SubTitleHolder(view);
            case TYPE_RECOMMEND_RECIPE:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_recommend_recipe, parent, false);
                return new RecommendRecipeHolder(view);
            case TYPE_TEXT_CP:
                view = LayoutInflater.from(mContext).inflate(R.layout.layout_textview, parent, false);
                return new TextCpHolder(view);
            case TYPE_FOOTER:
                view = LayoutInflater.from(mContext).inflate(R.layout.layout_foot_details, parent, false);
                return new FooterHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadInfoHolder) {
            HeadInfoHolder headInfoHolder = (HeadInfoHolder) holder;
            headInfoHolder.tvSchemeCommonHeadTitle.setText("膳食方案");
            if(foodDetailsBean != null) {
                headInfoHolder.tvSchemeCommonHeadContent.setText(foodDetailsBean.getGuide());
            }
        }

        if (holder instanceof RecommendRecipeHolder) {
            RecommendRecipeHolder recommendRecipeHolder = (RecommendRecipeHolder) holder;
            if(ITEM_BREAKFAST == position){
                recommendRecipeHolder.tvMeal.setText("早餐");
                setTextLeftDrawable(recommendRecipeHolder.tvMeal,R.drawable.recipe_breakfast);
                RecommendRecipeAdapter recommendRecipeAdapter = new RecommendRecipeAdapter(mContext,foodDetailsBean.getMorn());
                recommendRecipeHolder.recyclerView.setAdapter(recommendRecipeAdapter);
            }
            if(ITEM_LUNCH == position){
                recommendRecipeHolder.tvMeal.setText("午餐");
                setTextLeftDrawable(recommendRecipeHolder.tvMeal,R.drawable.recipe_lunch);
                RecommendRecipeAdapter recommendRecipeAdapter = new RecommendRecipeAdapter(mContext,foodDetailsBean.getNoon());
                recommendRecipeHolder.recyclerView.setAdapter(recommendRecipeAdapter);
            }
            if(ITEM_DINNER == position){
                recommendRecipeHolder.tvMeal.setText("晚餐");
                setTextLeftDrawable(recommendRecipeHolder.tvMeal,R.drawable.recipe_dinner);
                RecommendRecipeAdapter recommendRecipeAdapter = new RecommendRecipeAdapter(mContext,foodDetailsBean.getNight());
                recommendRecipeHolder.recyclerView.setAdapter(recommendRecipeAdapter);
            }

        }
        if (holder instanceof TextCpHolder) {
            TextCpHolder textCpHolder = (TextCpHolder) holder;
        }
    }

    @Override
    public int getItemCount() {
        return 7;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_HEAD_INFO;
            case 1:
                return TYPE_SUB_TITLE;
            case 5:
                return TYPE_TEXT_CP;
            case 6:
                return TYPE_FOOTER;
        }
        return TYPE_RECOMMEND_RECIPE;
    }


    class HeadInfoHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_scheme_common_head_title)
        TextView tvSchemeCommonHeadTitle;
        @Bind(R.id.tv_scheme_common_head_content)
        TextView tvSchemeCommonHeadContent;

        public HeadInfoHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class SubTitleHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_subtitle)
        TextView tvSubTitle;

        public SubTitleHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            tvSubTitle.setText("推荐食谱");
        }
    }

    class RecommendRecipeHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_meal)
        TextView tvMeal;
        @Bind(R.id.recycler_view_recipe)
        RecyclerView recyclerView;

        public RecommendRecipeHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            initView();
        }

        private void initView() {

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.setFocusable(false);
        }
    }


    class TextCpHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_cp)
        TextView tvCp;

        public TextCpHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            if(foodDetailsBean != null) {
                tvCp.setText(foodDetailsBean.getSuggest());
            }
        }
    }

    class FooterHolder extends RecyclerView.ViewHolder {

        public FooterHolder(View itemView) {
            super(itemView);
        }
    }

    private void setTextLeftDrawable(TextView textView,int resId){
        Drawable drawable= mContext.getResources().getDrawable(resId);
        /// 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(drawable,null,null,null);
    }
}
